local mumu = fk.CreateSkill{
  name = "bd__mumu",
}

Fk:loadTranslationTable{
  ["bd__mumu"] = "穆穆",
  [":bd__mumu"] = "出牌阶段开始时，你可以弃置场上一张牌且此牌须为武器牌或同势力角色场上的牌，若均满足，其摸两张牌。",
  
  ["#bd__mumu"] = "穆穆：弃置场上一张武器牌或同势力角色场上的牌",
}

local H = require "packages/ol_hegemony/util"

mumu:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(self) and data.phase == Player.Play
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getAlivePlayers(false), function(p)
      return H.compareKingdomWith(player, p) and #p:getCardIds("ej") > 0 or p:getEquipment(Card.SubtypeWeapon) ~= nil
    end)
    
    if #targets == 0 then return false end
    
    local to = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 1,
      prompt = "#bd__mumu",
      skill_name = mumu.name,
    })
    
    if #to > 0 then
      local target = to[1]
      local cards = target:getCardIds("ej")
      if not H.compareKingdomWith(player, target, false) then
        cards = target:getEquipments(Card.SubtypeWeapon)
      end
      if #cards == 1 then
        event:setCostData(self, {target = target, card = cards[1]})
        return true
      else
        local card = room:askToChooseCard(player, {
          target = target,
          flag = {card_data = {{"discard", cards}}},
          skill_name = mumu.name, 
        })
        if card then
          event:setCostData(self, {target = target, card = card})
          return true
        end
      end
    end
    return false
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    local target = cost_data.target
    local card = cost_data.card
    
    room:throwCard(card, mumu.name, target, player)
    
    local is_weapon = Fk:getCardById(card).type == Card.TypeEquip and Fk:getCardById(card).sub_type == Card.SubtypeWeapon
    local is_same_kingdom = H.compareKingdomWith(player, target)
    
    if is_weapon and is_same_kingdom then
       target:drawCards(2, mumu.name)
     end
  end,
})

return mumu